/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-09-01 09:15:51
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-10-10 17:40:18
 * @FilePath: \cxl-admin\src\pages\finance\billing\list\index.ts
 */
import { customeList, customeParasm } from '@/apis/finance/billing'
import { BaseData, BaseMethod } from '@/utils/basc-data'
import * as tableConfig from './config.json'
import { CreateXMLHttpRequest } from '@/utils'

export class SettleAccountsData extends BaseData implements BaseMethod {
    companyName = ''
    constructor() {
        super(tableConfig)

        if (this.uStore.isSuperAdmin) {
            this.pageData.search.unshift(['请输入客户编号/名称', '请输入客户编号/名称'])
        }

        this.getData()
    }

    onOperation = (arg?: any) => {
        const { type, data } = arg
        if (type === 'export') {
            let params = ''
            if (this.uStore.isSuperAdmin) {
                params = `current=99&size=99&queryContent=${this.companyName}`
            } else {
                params = `current=99&size=99&queryContent=${this.uStore.company}`
            }
            console.log('params', params)
            const url = import.meta.env.VITE_HOST + '/api/system/customerInfo/export?' + params

            CreateXMLHttpRequest(url, '开票列表')
        }
    }

    /**
     * 监听点击搜索按钮
     * @param res
     */
    onSearch = (res: any) => {
        this.companyName = res.custom.companyName
        this.getData()
    }

    /**
     * 监听点击搜索重置按钮
     */
    onReset = () => {
        this.getData()
    }

    /**
     * 监听分页发生改变
     * @param index
     */
    onPaginationChange = (index: number) => {
        this.pageData.pagination.pageNo = index
        this.getData()
    }

    /**
     * 监听选择每页显示个数
     * @param size
     */
    onPaginationSize = (size: number) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = size
        this.getData()
    }

    /**
     * 拉取数据
     */
    getData() {
        const params: customeParasm = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize
        }
        if (this.companyName) {
            params.queryContent = this.companyName
        }
        customeList(params).then(res => {
            if (res.data.value === 200) {
                console.log(res.data.data)
                this.pageData.table.data.length = 0
                this.pageData.table.data.push(...res.data.data.records)
                this.pageData.pagination.total = res.data.data.total
            }
        })
    }
}
